#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : vendor
# @IDE            : PyCharm
# @desc           : 描述

from typing import List, Optional
from pydantic import BaseModel, ConfigDict, root_validator
from core.data_types import DatetimeStr


class OrderContent(BaseModel):
    order_id: int

    test_name: str
    attachment: Optional[str] = None
    is_pass: Optional[bool] = None
    test_performed_by: Optional[str] = None

    operation: Optional[str] = None

    create_user: Optional[str] = None


class OrderProgress(BaseModel):
    order_id: int
    content: str
    status: str

    create_user: Optional[str] = None


class Order(BaseModel):
    order_type: str
    order_no: str

    qar_no: str
    product_type: str
    part_type: str
    form_type: str

    status: Optional[str] = None

    prepared_by: Optional[str] = None
    prepared_date: Optional[DatetimeStr] = None
    prepared_dept: Optional[str] = None
    prepared_phone: Optional[str] = None

    material_description: Optional[str] = None

    vendor: Optional[str] = None
    po: Optional[str] = None

    test_document: Optional[str] = None
    lot_size: Optional[str] = None

    approved_by: Optional[str] = None
    
    meet_spec_requirements: Optional[str] = None
    
    received_by: Optional[str] = None
    feeded_by: Optional[str] = None

    test_type: Optional[str] = None
    is_urgent: Optional[bool] = None
    is_heavy_urgent: Optional[bool] = None
    finished_date: Optional[DatetimeStr] = None
    
    require_finish_datetime: Optional[DatetimeStr] = None

    create_user: Optional[str] = None


class OrderUpdate(BaseModel):
    prepared_by: Optional[str] = None
    prepared_date: Optional[DatetimeStr] = None
    prepared_dept: Optional[str] = None
    prepared_phone: Optional[str] = None

    status: Optional[str] = None
    lot_size: Optional[str] = None

    approved_by: Optional[str] = None
    
    meet_spec_requirements: Optional[str] = None
    
    received_by: Optional[str] = None
    feeded_by: Optional[str] = None

    test_type: Optional[str] = None
    is_urgent: Optional[bool] = None
    is_heavy_urgent: Optional[bool] = None
    
    test_document: Optional[str] = None
    vendor: Optional[str] = None

    require_finish_datetime: Optional[DatetimeStr] = None


class OrderProgressSimpleOut(OrderProgress):
    model_config = ConfigDict(from_attributes=True)

    id: int

    update_datetime: DatetimeStr
    create_datetime: DatetimeStr


class OrderContentSimpleOut(OrderContent):
    model_config = ConfigDict(from_attributes=True)

    id: int

    update_datetime: DatetimeStr
    create_datetime: DatetimeStr


class OrderSimpleOut(Order):
    model_config = ConfigDict(from_attributes=True)

    id: int

    update_datetime: DatetimeStr
    create_datetime: DatetimeStr


class OrderOut(OrderSimpleOut):
    model_config = ConfigDict(from_attributes=True)

    progresses: Optional[List[OrderProgressSimpleOut]] = []
    contents: Optional[List[OrderContentSimpleOut]] = []

    @root_validator(pre=True)
    def convert(cls, v):
        progresses = [i for i in v.progresses if i.is_delete]
        contents = [i for i in v.contents if not i.is_delete]
        data = OrderSimpleOut.model_validate(v).model_dump()
        return dict(progresses=progresses, contents=contents, **data)


class OrderUpdateStatus(BaseModel):
    status: Optional[str] = None


class OrderStampType(BaseModel):
    type: str
    